home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows 95 API Bible
/
Windows 95 API Bible 3 Disc Set.iso
/
Win32 API Bible Book 3 of 3.iso
/
chapte13
/
cmcmail.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-03-07
|
12KB
|
413 lines
#include <windows.h>
#include <commctrl.h>
#include "cmc.h"
#include "CMCMail.h"
#if defined (WIN32)
#define IS_WIN32 TRUE
#else
#define IS_WIN32 FALSE
#endif
#define IS_NT IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
#define IS_WIN32S IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
#define IS_WIN95 (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
HINSTANCE hInst; // current instance
LPCTSTR lpszAppName = "MyApp";
LPCTSTR lpszTitle = "CMC Mail";
BOOL RegisterWin95( CONST WNDCLASS* lpwc );
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
HWND hWnd;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (hInstance, lpszAppName);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = lpszAppName;
wc.lpszClassName = lpszAppName;
if ( IS_WIN95 )
{
if ( !RegisterWin95( &wc ) )
return( FALSE );
}
else if ( !RegisterClass( &wc ) )
return( FALSE );
hInst = hInstance;
hWnd = CreateWindow( lpszAppName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0,
NULL,
NULL,
hInstance,
NULL
);
if ( !hWnd )
return( FALSE );
ShowWindow( hWnd, nCmdShow );
UpdateWindow( hWnd );
while( GetMessage( &msg, NULL, 0, 0) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return( msg.wParam );
}
BOOL RegisterWin95( CONST WNDCLASS* lpwc )
{
WNDCLASSEX wcex;
wcex.style = lpwc->style;
wcex.lpfnWndProc = lpwc->lpfnWndProc;
wcex.cbClsExtra = lpwc->cbClsExtra;
wcex.cbWndExtra = lpwc->cbWndExtra;
wcex.hInstance = lpwc->hInstance;
wcex.hIcon = lpwc->hIcon;
wcex.hCursor = lpwc->hCursor;
wcex.hbrBackground = lpwc->hbrBackground;
wcex.lpszMenuName = lpwc->lpszMenuName;
wcex.lpszClassName = lpwc->lpszClassName;
// Added elements for Windows 95.
//...............................
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName,
IMAGE_ICON, 16, 16,
LR_DEFAULTCOLOR );
return RegisterClassEx( &wcex );
}
HWND hList = NULL;
CMC_session_id lhSession = 0;
LPCTSTR pszColumns[] = { "Sender", "Subject", "Date" };
VOID RefreshContents()
{
CMC_message_summary* result = NULL;
CMC_message_reference* reference = NULL;
int nPos = 0;
CMC_uint32 uCount = 1;
// Clear the list view contents.
//..............................
ListView_DeleteAllItems( hList );
// Continue until there are no more items.
//........................................
while ( !CMCList( lhSession, NULL, CMC_ERROR_UI_ALLOWED,
( reference ? reference : NULL ), &uCount,
0, &result, NULL ) )
{
LV_ITEM item;
TCHAR szDate[32];
// If there are no more items, break.
//...................................
if ( uCount == 0 )
break;
reference = (CMC_message_reference*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
sizeof( CMC_message_reference )+
result->message_reference->length );
memcpy( reference, result->message_reference, sizeof( CMC_message_reference )+
result->message_reference->length );
item.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
item.iItem = nPos;
item.iImage = 0;
item.iSubItem = 0;
item.pszText = result->originator->name;
item.lParam = (LPARAM)reference;
ListView_InsertItem( hList, &item );
ListView_SetItemText( hList, nPos, 1, result->subject );
wsprintf( szDate, "%2d/%2d/%4d", result->time_sent.month+1,
result->time_sent.day,
result->time_sent.year+1900 );
ListView_SetItemText( hList, nPos, 2, szDate );
CMCFree( result );
result = NULL;
}
}
VOID DeleteMessage()
{
LV_ITEM item;
int nIndex = -1;
CMC_message_reference* reference;
while( (nIndex = ListView_GetNextItem( hList, nIndex, LVNI_SELECTED )) > -1 )
{
item.mask = LVIF_PARAM;
item.iItem = nIndex;
item.iSubItem = 0;
ListView_GetItem( hList, &item );
reference = (CMC_message_reference*)item.lParam;
// Delete the item from the mailbox.
//..................................
if ( !CMCActOn( lhSession, reference, CMC_ACT_ON_DELETE, CMC_ERROR_UI_ALLOWED, 0, NULL ) )
HeapFree( GetProcessHeap(), 0, reference );
}
RefreshContents();
}
VOID ReadMessage()
{
LV_ITEM item;
int nIndex = -1;
CMC_message_reference* reference;
// Get the selected list view item.
//.................................
if( (nIndex = ListView_GetNextItem( hList, nIndex, LVNI_SELECTED )) > -1 )
{
CMC_message* message = NULL;
item.mask = LVIF_PARAM;
item.iItem = nIndex;
item.iSubItem = 0;
ListView_GetItem( hList, &item );
reference = (CMC_message_reference*)item.lParam;
// Retrieve the message.
//......................
if ( !CMCRead( lhSession, reference, CMC_ERROR_UI_ALLOWED, &message, 0, NULL ) )
{
// Display the message contents.
//..............................
MessageBox( hList, message->text_note, message->subject, MB_OK );
CMCFree( message );
}
}
}
VOID SendMailMessage()
{
CMC_message message;
memset( &message, 0, sizeof( message ) );
if ( !CMCSend( lhSession, &message, CMC_SEND_UI_REQUESTED, 0, NULL ) )
MessageBox( hList, "Message was sent.", "Send Message", MB_OK | MB_ICONINFORMATION );
else
MessageBox( hList, "Message could not be sent.", "Send Message", MB_OK | MB_ICONINFORMATION );
}
VOID ShowProperties()
{
LV_ITEM item;
int nIndex = -1;
CMC_message_reference* reference = NULL;
CMC_message_summary* result = NULL;
// Get the selected list view item.
//.................................
if( (nIndex = ListView_GetNextItem( hList, nIndex, LVNI_SELECTED )) > -1 )
{
CMC_message* message = NULL;
CMC_uint32 uCount = 1;
if ( nIndex > 0 )
{
item.mask = LVIF_PARAM;
item.iItem = nIndex;
item.iSubItem = 0;
ListView_GetItem( hList, &item );
reference = (CMC_message_reference*)item.lParam;
}
// Retrieve the message.
//......................
if ( !CMCList( lhSession, NULL, CMC_ERROR_UI_ALLOWED,
( reference ? reference : NULL ), &uCount,
0, &result, NULL ) )
{
CMC_recipient* recipient = NULL;
// Show properties of sender.
//...........................
CMCLookup( lhSession, result->originator, CMC_LOOKUP_DETAILS_UI,
0, &uCount, &recipient, NULL );
CMCFree( recipient );
}
}
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch( uMsg )
{
case WM_CREATE :
InitCommonControls();
if ( !CMCInitialize() )
{
MessageBox( hWnd, "CMC is not supported!", NULL, MB_OK | MB_ICONSTOP );
return( -1 );
}
hList = CreateWindowEx( WS_EX_CLIENTEDGE, WC_LISTVIEW, "",
WS_CHILD | WS_VISIBLE | LVS_REPORT,
0, 0, 10, 10, hWnd, (HMENU)101, hInst, NULL );
if ( hList )
{
CMC_boolean bUIAvail;
HIMAGELIST hImage;
LV_COLUMN lvc;
int index;
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvc.fmt = LVCFMT_LEFT;
lvc.cx = 150;
for (index = 0; index <= 2; index++)
{
lvc.iSubItem = index;
lvc.pszText = (LPTSTR)pszColumns[index];
ListView_InsertColumn( hList, index, &lvc );
}
hImage = ImageList_Create( 16, 16, ILC_COLOR | ILC_MASK, 1, 1 );
ImageList_AddIcon( hImage, LoadIcon( hInst, "RECMAIL" ) );
ListView_SetImageList( hList, hImage, LVSIL_SMALL );
// Connect to mail service.
//.........................
if ( (CMCQueryConfiguration( 0, CMC_CONFIG_UI_AVAIL,
&bUIAvail, NULL) == CMC_SUCCESS) && bUIAvail )
{
if ( CMCLogon( NULL, NULL, NULL, NULL, (CMC_ui_id) NULL,
CMC_VERSION, CMC_LOGON_UI_ALLOWED | CMC_ERROR_UI_ALLOWED,
&lhSession, NULL ) )
{
MessageBox( hWnd, "Could not log on to mail service!", NULL,
MB_OK | MB_ICONSTOP );
CMCUninitialize();
return( -1 );
}
}
RefreshContents();
}
break;
case WM_COMMAND :
switch( LOWORD( wParam ) )
{
case IDM_REFRESH :
RefreshContents();
break;
case IDM_DELETE :
DeleteMessage();
break;
case IDM_OPEN :
ReadMessage();
break;
case IDM_NEW :
SendMailMessage();
break;
case IDM_PROPERTIES :
ShowProperties();
break;
case IDM_ABOUT :
DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
break;
case IDM_EXIT :
DestroyWindow( hWnd );
break;
}
break;
case WM_SIZE :
MoveWindow( hList, 0, 0, LOWORD( lParam ), HIWORD( lParam ), TRUE );
break;
case WM_DESTROY :
if ( lhSession > 0 )
CMCLogoff( lhSession, (CMC_ui_id) NULL, 0, 0 );
CMCUninitialize();
PostQuitMessage(0);
break;
default :
return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
}
return( 0L );
}
LRESULT CALLBACK About( HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return (TRUE);
case WM_COMMAND:
if ( LOWORD(wParam) == IDOK
|| LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, TRUE);
return (TRUE);
}
break;
}
return (FALSE);
}